<?php
session_start();
//   session_destroy();
//var_dump($_SESSION);

include_once 'inc/classes.inc.php';

if (empty($_SESSION)) {
    header('location: ../index.php');
} else {
    if (!isset($_SESSION['user']))
        header('location: ../index.php');
    else {
        $avisDAO = new AvisDAO();
        $userDAO = new UserDAO();
        $commmandeDAO = new CommandesDAO();

        $produitDAO = new ProduitDAO();
        $lesAvis = array();
        $user = unserialize($_SESSION['user']);
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <?php
        include_once 'inc/entete.php';
        ?>
    </head>
    <body>
        <?php
        include_once '../inc/modal.php';
        include_once '../inc/header.php';
        ?>

        <!--LES TABS-->

        <ul class="menutab nav nav-tabs" id="menu">
            <li class="active"><a href="#lesutilisateurs">Les utilisateurs</a></li>
            <li><a href="#lesCommandes">Les Commandes</a></li>
            <li><a href="#lesProduits">Les Produits</a></li>
            <li><a href="#lesavis">Les avis</a></li>


        </ul>
        <!-------------CONTENU TAB---------->
        <div class="tab-content">
            <!-------------TOUTES LES COMMANDES---------->
            <div class="tab-pane" id="lesCommandes">

                <?php
//                    var_dump($commmandeDAO->getCommandesByIdUser($user->getId()));
                $lesCommandes = $commmandeDAO->getToutesLesCommandes();
//            var_dump($lesCommandes);
//            if ($lesCommandes != null) {
                $cpt = 0;
//                            var_dump($lesCommandes);
                foreach ($lesCommandes as $commande) {
                    echo " <div class='accordion-group'>";
                    echo "<div class='accordion-heading'>";
                    echo "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion2' href='#collapse$cpt'>";
                    echo "Commande N° " . $commande->getId() . " / " . $commande->getDate() . " / " . $commande->getPrixTotal() . "&nbsp;€ <span class='label label-info pull-right'>" . $commande->getStatut() . "</span>";
                    echo "</a>";
                    echo "<div id='collapse$cpt' class='accordion-body collapse'>";
                    echo "<div  class='accordion-inner'>";
                    echo "<table class='table table-striped'>
                                                        <tr>
                                                            <th>Img</th>
                                                            <th>Nom</th>
                                                            <th>Prix</th>
                                                            <th>Quantite</th>
                                                            <th>total</th>
                                                        </tr>";

                    foreach ($commande->getLesProduitPanier() as $produitComm) {
                        $pdt = $produitComm->getProduitPanier();
                        echo "<tr>";
                        echo "<td><a href='visuproduit.php?idp=" . $pdt->getReference() . "'><img src='../" . $pdt->getCheminImage() . "' width='75'></a></td>";
                        echo "<td><a href='visuproduit.php?idp=" . $pdt->getReference() . "'>" . $pdt->getNom() . "</a></td>";
                        echo "<td>" . $pdt->getNom() . "</td>";
                        echo "<td>" . $produitComm->getQuantiteProduit() . "</td>";
                        echo "<td>" . $produitComm->getPrixTotalUnitaire() . "&nbsp;€</td>";
                        echo "</tr>";
                    }
                    echo "</table>";
                    echo "</div>";
                    echo "</div>";
                    echo "</div>";
                    echo "</div>";

                    $cpt++;
                }
//            } else {
//                echo "<p class='alert alert-info center'>Vous n'avez pas passé aucune commande</p>";
//            }
                ?>
            </div>
            <!-------------TOUS LES UTILISATEURS---------->
            <div class="tab-pane active" id="lesutilisateurs">

                <?php
                $tousLesUser = $userDAO->getAllUser();
//                var_dump($tousLesUser);
                echo "<table class='table table-striped'>
                        <tr>
                            <th>id</th>
                            <th>Nom</th>
                            <th>Prénom</th>
                            <th>Login</th>
                            <th>mot de passe</th>
                            <th>Email</th>
                            <th>Date de Naissance</th>
                            <th>Administrateur</th>
                            <th>Action</th>
                        </tr>";
                foreach ($tousLesUser as $utilisateur) {
                    echo "<tr><td>" . $utilisateur->getid() . "</td>";
                    echo "<td>" . $utilisateur->getNom() . "</td>";
                    echo "<td>" . $utilisateur->getPrenom() . "</td>";
                    echo "<td>" . $utilisateur->getLogin() . "</td>";
                    echo "<td>" . $utilisateur->getMdp() . "</td>";
                    echo "<td>" . $utilisateur->getEmail() . "</td>";
                    echo "<td>" . $utilisateur->getDateNaissance() . "</td>";
                    echo "<td>" . $utilisateur->estAdmin() . "</td>";
                    echo"<td><a class='btn btn-warning'><i class='icon-edit icon-white'></i>&nbsp;</a></td></tr>";
                    
                }
                echo "</table>";
                ?>

            </div>
            <!-------------TOUS LES AVIS---------->
            <div class="tab-pane" id="lesavis">

                <?php
                $lesavis = $avisDAO->getLesAvis();
                
                echo "<table class='table table-striped'>
                        <tr>
                            <th>id</th>
                            <th>Nom</th>
                            <th>Date de publication</th>
                            <th>commentaire</th>
                            <th>note</th>
                            <th>valide</th>
                            <th>Action</th>
                        </tr>";
                foreach ($lesavis as $unavis) {
                    
                    echo "<tr><td>" . $unavis->getid() . "</td>";
                    echo "<td>" . $unavis->getProduit()->getNom() . "</td>";
                    echo "<td>" . $unavis->getDatePublication() . "</td>";
                    echo "<td>" . $unavis->getCommentaire() . "</td>";
                    echo "<td>" . $unavis->getNote() . "</td>";
                    echo "<td>" . $unavis->estValide() . "</td>";
                    echo"<td><a class='btn btn-warning'><i class='icon-edit icon-white'></i>&nbsp;</a></td></tr>";

//                echo "<td>".$unavis->getAuteur()->getNom()."</td>";
                    
                }
                echo "</table>";
                ?>
            </div>
            <!-------------TOUS LES PRODUITS---------->
            <div class="tab-pane" id="lesProduits">
                <?php
                $produits = new ProduitDAO();
                $getLesProduits = $produits->getAllProduits();
//            var_dump($getLesProduits);

                echo '
             <table class="table table-striped">
                <tr>
                  <th>#</th>
                  <th>Nom</th>                  
                  <th>Image</th>
                  <th>Note produit</th>
                  <th>Prix</th>
                  <th>Action</th>
                </tr>';
                foreach ($getLesProduits as $value) {
                    echo"<tr><td>" . $value->getReference() . "</td>";
                    echo"<td>" . $value->getNom() . "</td>";
                    echo"<td><a href='visuproduit.php?idp=" . $value->getReference() .
                    "' target=_blank><img src='../" . $value->getCheminImage() . "' width='125'></a></td>";
                    echo"<td>" . $value->getNoteProduit() . "</td>";
                    echo"<td>" . $value->getPrix() . "</td>";
                    echo"<td><a class='btn btn-warning' href='visuproduit.php?idp=" . $value->getReference() ."'><i class='icon-edit icon-white'></i>&nbsp;</a></td></tr>";
                }
                echo "</table>";
                ?>

            </div>

        </div>
    </body>
</html>